const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    page:1,//分页
    limit: 5,//一页展示数量
    flag: true,
    datafind:"",//搜索的关键字
    __returnParams:null,//刷新
    setManager:null,
    showBtn:true,
    tyepList:[],
    selType:-1,
    date:"",
    mindate:Math.round((new Date() - 365*86400*1000)),
    show:false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.show === "false"){
      this.setData({showBtn:false})
    }
    if(options.select!=undefined){
      var select = options.select>=1?true:false;
      this.setData({select:select})
    }
    this.getList();
    this.type_data()
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  onDisplay() {
    this.setData({ show: true });
  },
  
  onClose() {
    this.setData({ show: false });
  },

  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
    this.getList("",this.data.date)
  },

  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onSearch(e){
    this.setData({
      flag:true
    })
    this.data.page = 1
    this.data.datafind = e.detail
    this.getList()
  },

  onReachBottom() {
    if(this.data.flag){
      this.setData({
        flag:false
      })
      this.getList(); // 疯狂的请求的方法
    }
  },

  type_data(){
    // var that = this
    // request.requestGet('material/get_type_data', "", "加载中",function (res) {
    //   console.log(res,"resresres")
    //   if (res.status === 200) {
    //     console.log(res,"获取分类")
    //     that.setData({
    //       tyepList:res.data,
    //       activeTab: 0
    //     })
    //   }
    //   }, function (res) {
    //     console.log(res,"请求失败")
    //   },
    // )
  },

  getList() {
    var that = this;
    var params = {
      page: that.data.page,//当前页码
      keyword:that.data.datafind,//关键字
      limit: that.data.limit,
      type:that.data.selType,
    }

    request.requestGet('order/get_sale_orders', params, "加载中",function (res) {
      console.log(res,"resssssssss")
      if (res.status === 200) {
        let list = that.data.list
        res.data.data.map((item) =>{
          return item.t = false;
        })
        if (that.data.page == 1) {
          list = []
        }
        list = [...list, ...res.data.data]
        if (list.length < res.data.count ){
          that.setData({
            page: that.data.page + 1,
            flag:true
          })
        } else {
          that.setData({
            flag:false
          })
        }
        that.setData({
          list: list,
          count: res.data.count
        })
      }
      }, function (res) {
        console.log(res,"请求失败")
      },
    )


    // request.requestGet('order/get_sale_orders', params, "加载中",function (res) {
    //   console.log(res,"resresres")
    //   if (res.status === 200) {
    //     let list = that.data.list
    //     res.data.data.map((item) =>{
    //       return item.t = false;
    //     })
    //     if (that.data.page == 1) {
    //       list = []
    //     }
    //     list = [...list, ...res.data.data]
    //     if (list.length < res.data.count ){
    //       that.setData({
    //         page: that.data.page + 1,
    //         flag:true
    //       })
    //     } else {
    //       that.setData({
    //         flag:false
    //       })
    //     }
    //     that.setData({
    //       list: list,
    //       count: res.data.count
    //     })

    //   }
    //   }, function (res) {
    //     console.log(res,"请求失败")
    //   },
    // )
  },

  editor(e){
    var index = e.currentTarget.dataset.index
    var tmpinfo = this.data.list[index]
    for(let i = 0;i<this.data.list.length;i++){
      if (this.data.list[i].id === index){
        tmpinfo = this.data.list[i]
      }
    }
    wx.navigateTo({
      url: `/pages/edi_materials/edi_materials?value1=${tmpinfo.name}&value2=${tmpinfo.attr}&value3=${tmpinfo.no}&value4=${tmpinfo.unit}&value5=${tmpinfo.report_line}&value6=${index}&storage_location=${tmpinfo.storage_location}&type_label=${tmpinfo.type_label}`,
    })
  },
  setgocome(e){
    wx.navigateTo({
      url: `/pages/setgocome/setgocome?id=${e.currentTarget.dataset.index}`,
    })
  },
  recordremember(e){
    wx.navigateTo({
      url: `/pages/inventory_record/inventory_record?id=${e.currentTarget.dataset.index}`,
    })
  },
  delinfo(e){
    var that = this;
      wx.showModal({
        title: '提示',
        content: '确定删除当前数据吗？',
        success: function (res) {
          if (res.confirm) {
            that.delData(e)
          } 
        }
      })
  },
  addinfo(e){
    wx.navigateTo({
      url: `/pages/edi_materials/edi_materials`,
    })
  },
  delData(data){
    var that = this;
    var index = data.currentTarget.dataset.index;
    let tmp = {id:index}
    request.requestGet('material/delete', tmp, '正在删除',function (res) {
      if (res.status === 200) {
        for(let i = 0;i<that.data.list.length;i++){
          if (that.data.list[i].id === index){
            index = i
          }
        }
        let list = that.data.list;
        list.splice(index,1)
        that.setData({
          list:list
        })
        wx.showToast({
          title: '删除成功',
          icon:'success'
        });
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    }, function (res) {
      wx.showToast({
        title: '删除失败',
        icon:'none'
      })
    })
  },
  onshow(){
    if (this.data.__returnParams) {
      this.getList()
      this.setData({ 
        __returnParams: null 
      }) // 清空避免重复触发
    }
  },


  getList1() {
    var that = this;
    this.data.page = 1
    this.data.limit = 5
    var params = {
      page: this.data.page,//当前页码
      keyword:"",//关键字
      limit: this.data.limit,
    }
    that.data.list = []
    request.requestGet('material/get_material_list', params, "加载中",function (res) {
      if (res.status === 200) {
        let list = that.data.list
        res.data.data.map((item) =>{
          return item.t = false;
        })
        if (that.data.page == 1) {
          list = []
        }
        list = [...list, ...res.data.data]
        if (list.length < res.data.count ){
          that.setData({
            page: that.data.page + 1,
            flag:true
          })
        } else {
          that.setData({
            flag:false
          })
        }
        that.setData({
          list: list,
          count: res.data.count
        })

      }
      }, function (res) {
        console.log(res,"请求失败")
      },
    )
  },
  retsel(e){
    var that = this
    let index = e.currentTarget.dataset.index
    let name = that.data.list[index].name
    let attr = that.data.list[index].attr
    let id = that.data.list[index].id
    const pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    if (pages.length >= 2) {
      let tmplist = [name,attr,id]
      pages[pages.length - 2].setData({
        setManager: tmplist
      }, () => {
        wx.navigateBack() // 确保setData回调完成后再返回
        prevPage.setManager()
      })
    }
    
  },

  asdfqwe(){
    
  }
  // onTabChange(e){
  //   var that = this
  //   console.log(e,"选择的")
  //   let tmpNum = -1
  //   if(e.detail.index === 0){
  //     tmpNum = -1
  //     that.setData({
  //       selType:-1,
  //       page:1
  //     })
  //   }else if(e.detail.index === 1){
  //     tmpNum = 0
  //     that.setData({
  //       selType:0,
  //       page:1
  //     })

  //   }else if(e.detail.index === 2){
  //     that.setData({
  //       selType:1,
  //       page:1
  //     })

  //   }else if(e.detail.index === 3){
  //     tmpNum = 2
  //     that.setData({
  //       selType:2,
  //       page:1
  //     })
  //   }
  //   this.getList()
  // }
})